<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
session_start();
include 'dynamic/cart.php';
include 'logic/Connection.php';
$cartObj = new cart();
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Your Shoping Cart</title>
    </head>
    <body>
        <?php
        $cart = $_SESSION['cart'];
        $action = $_GET['action'];
        switch ($action) {
            case 'add':
                if ($cart) {
                    $cart .= ','.$_GET['id'];
                } else {
                    $cart = $_GET['id'];
                }
                break;
            case 'delete':
                if ($cart) {
                    $items = explode(',',$cart);
                    $newcart = '';
                    foreach ($items as $item) {
                        if ($_GET['id'] != $item) {
                            if ($newcart != '') {
                                $newcart .= ','.$item;
                            } else {
                                $newcart = $item;
                            }
                        }
                    }
                    $cart = $newcart;
                }
                break;
            case 'update':
                if ($cart) {
                    $newcart = '';
                    foreach ($_POST as $key=>$value) {
                        if (stristr($key,'qty')) {
                            $id = str_replace('qty','',$key);
                            $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
                            $newcart = '';
                            foreach ($items as $item) {
                                if ($id != $item) {
                                    if ($newcart != '') {
                                        $newcart .= ','.$item;
                                    } else {
                                        $newcart = $item;
                                    }
                                }
                            }
                            for ($i=1;$i<=$value;$i++) {
                                if ($newcart != '') {
                                    $newcart .= ','.$id;
                                } else {
                                    $newcart = $id;
                                }
                            }
                        }
                    }
                }
                $cart = $newcart;
                break;

        }
        $_SESSION['cart'] = $cart;
        ?>

        <h1>Your Shopping Cart</h1>
        <?php
        echo $cartObj->writeShoppingCart();
        ?>

        <h1>Please check quantities...</h1>

        <?php
        echo $cartObj->showCart();
        
        echo '<a href="' . $_SERVER['HTTP_REFERER'].'">Back to Shopping</a>';
        ?>


    </body>
</html>
